﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.Core.Util;
using FSS.Core.Controls;
using FSS.BLL.Services;
using FSS.BLL.Entities;
using System.Collections;
using FSS.BLL;

namespace SalesManagement.Modules.Sales
{
    public partial class VoucherOutDetail : BasePage
    {
        public VoucherOutDetail()
            : base(SCREENS.VOUCHEROUTDETAIL)
        {

        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [DirectMethod]
        public void DocumentReady()
        {
            BindingCustomer();
            BindingMaturity();
            BindingInputMethod();
            BindingEmployee();
            BindingCurrency();
            BindingPaymentMethod();
            BindingInvoiceType();
            BindingUnit();
            BindingWaveHouse();
            BindingDocument();

        }
        [DirectMethod]
        public bool NewDataProcess()
        {
            ConfigNumberService configNumberService = ConfigNumberService.GetInstance();
            txtVoucherNo.Text = configNumberService.GetNumberCode(Constants.VoucherType.PXK.ToString());
            dfVoucherDate.SelectedDate = DateTime.Now;
            txtAddress.Text = string.Empty;
            //txtDocumentNo.Text = string.Empty;
            //txtInvoiceNo.Text = string.Empty;
            //txtTaxID.Text = string.Empty;
            List<VoucherDetailEntity> resul = new List<VoucherDetailEntity>();
            stDetail.DataBind();
            btnNew.Disabled = true;
            return true;
        }
        private void BindingDocument()
        {
            Int64 VoucherId = Format.ConvertToLong(hdVoucherId.Text);
            if (VoucherId > 0)
            {
                VoucherService service = VoucherService.GetInstance();
                VoucherEntity resul = service.GetEntityByPrimaryKey(VoucherId);
                if (resul == null)
                    return;
                txtVoucherNo.Text = resul.VoucherNo;
                dfVoucherDate.SelectedDate = resul.VocherDate;
                cboObject.SelectedItem.Value = resul.ObjectId.ToString();
                //cbPaymentMethod.SelectedItem.Value = resul.PaymentMethodId.ToString();
                cboMaturity.SelectedItem.Value = resul.MaturityId.ToString();
                //cbCurrency.SelectedItem.Value = resul.CurrencyId.ToString();
                //nfRate.SetValue(resul.CurrencyRate);
                cboInputMethod.SelectedItem.Value = resul.InOutMethodId.ToString();
                VoucherDetailService svDetail = VoucherDetailService.GetInstance();
                IList<VoucherDetailEntity> lstDetail = svDetail.GetAllDetailByMasterID(VoucherId);
                stDetail.DataSource = lstDetail;
                stDetail.DataBind();
                btnNew.Disabled = false;
            }
            else
                NewDataProcess();

        }
        #region binding data
        /// <summary>
        /// Gan bao gia
        /// </summary>
        [DirectMethod]
        public void BindingQuotation(int objectId)
        {
            if (objectId <= 0) return;
            QuotationEntity quotationEntity = new QuotationEntity();
            quotationEntity.ObjectId = objectId;
            QuotationService quotationService = QuotationService.GetInstance();
            List<QuotationEntity> lstQuotation = (List<QuotationEntity>)quotationService.LoadByObjectId(quotationEntity);
            if (lstQuotation == null)
                lstQuotation = new List<QuotationEntity>();
            stQuotation.DataSource = lstQuotation;
            stQuotation.DataBind();
            if (lstQuotation.Count > 0)
                cboQuotation.SelectedIndex = 0;
            else
            {
                cboQuotation.Text = string.Empty;
            }

        }
        /// <summary>
        /// Gan nha cung cap
        /// </summary>
        private void BindingCustomer()
        {
            ObjectService objectService = ObjectService.GetInstance();
            IList<ObjectEntity> lstObject = objectService.GetAllProvider();
            stObject.DataSource = lstObject;
            stObject.DataBind();
            if (lstObject.Count > 0)
            {
                txtTaxCode.Text = lstObject[0].TaxCode;
                txtAddress.Text = lstObject[0].Address;
                cboObject.SelectedIndex = 0;
                BindingQuotation(lstObject[0].ObjectId);
            }
        }
        /// <summary>
        /// Binding Han thanh toán
        /// </summary>
        private void BindingMaturity()
        {
            MaturityService service = MaturityService.GetInstance();
            IList<MaturityEntity> Resul = service.LoadAll();

            stMaturity.DataSource = Resul;
            stMaturity.DataBind();
            if (Resul.Count > 0)
                cboMaturity.SelectedIndex = 0;
        }
        private void BindingInputMethod()
        {
            InOutMethodService service = InOutMethodService.GetInstance();
            IList<InOutMethodEntity> lstInOutMethod = service.LoadAll();
            stInOutMethod.DataSource = lstInOutMethod;
            stInOutMethod.DataBind();
            if (lstInOutMethod.Count > 0)
                cboInputMethod.SelectedIndex = 0;
        }
        private void BindingEmployee()
        {
            EmployeeService service = EmployeeService.GetInstance();
            IList<EmployeeEntity> Resul = service.LoadAll();
            if (Resul == null)
                return;
            stEmployee.DataSource = Resul;
            stEmployee.DataBind();
            if (Resul.Count > 0)
                cboEmployee.SelectedIndex = 0;
        }
        private void BindingCurrency()
        {
            CurrencyService service = CurrencyService.GetInstance();
            IList<CurrencyEntity> Resul = service.LoadAll();
            if (Resul == null)
                return;
            stCurrency.DataSource = Resul;
            stCurrency.DataBind();
            //if (Resul.Count > 0)
            //cboCurrency.SelectedIndex = 0;
        }
        /// <summary>
        /// Binding Phuong thức thanh toán
        /// </summary>
        private void BindingPaymentMethod()
        {
            PaymentMethodService service = PaymentMethodService.GetInstance();
            IList<PaymentMethodEntity> Resul = service.LoadAll();
            if (Resul == null)
                return;
            stPaymentMethod.DataSource = Resul;
            stPaymentMethod.DataBind();
            if (Resul.Count > 0)
                cboPaymentMethod.SelectedIndex = 0;
        }
        /// <summary>
        /// Binding Loai hinh hoa don
        /// </summary>
        private void BindingInvoiceType()
        {
            InvoiceTypeService invoiceTypeService = InvoiceTypeService.GetInstance();
            IList<InvoiceTypeEntity> lstInvoiceType = invoiceTypeService.LoadAll();
            if (lstInvoiceType == null)
                return;
            stInvoiceType.DataSource = lstInvoiceType;
            stInvoiceType.DataBind();
            if (lstInvoiceType.Count > 0)
                cboInvoiceType.SelectedIndex = 0;
        }
        /// <summary>
        /// Binding Đơn vị tính
        /// </summary>
        private void BindingUnit()
        {
            UnitService service = UnitService.GetInstance();
            IList<UnitEntity> Resul = service.LoadAll();
            if (Resul == null)
                return;
            stUnit.DataSource = Resul;
            stUnit.DataBind();

        }


        private void BindingWaveHouse()
        {
            WareHouseService wareHouseService = WareHouseService.GetInstance();
            IList<WareHouseEntity> lstWareHouseEntity = wareHouseService.LoadAll();
            if (lstWareHouseEntity == null)
                return;
            stWareHouse.DataSource = lstWareHouseEntity;
            stWareHouse.DataBind();
            if (lstWareHouseEntity.Count > 0)
                cboWarehouse.SelectedIndex = 0;

        }

        #endregion
        [DirectMethod]
        public Int64 SaveVoucher()
        {
            try
            {
                VoucherEntity voucher = GetVoucherEntity();
                VoucherService voucherService = VoucherService.GetInstance();
                Int64 VoucherId = Format.ConvertToLong(hdVoucherId.Text);
                voucher.VoucherType = Constants.VoucherType.PXK.ToString();
                if (VoucherId > 0)
                    voucher.VoucherId = VoucherId;
                if (VoucherId <= 0)
                {
                    VoucherId = voucherService.InsertUpdate(voucher);
                }

                if (VoucherId < 0)
                    return -1;
                else
                {
                    hdVoucherId.Text = VoucherId.ToString();
                    return VoucherId;
                }
                //if (string.IsNullOrEmpty(JsonDetail))
                //    return -2;
                //Dictionary<string, string> data = JSON.Deserialize<Dictionary<string, string>[]>(JsonDetail)[0];
                //VoucherDetailEntity ogroup = new VoucherDetailEntity();
                //ogroup.VoucherDetailId = Format.ConvertToInt(data["VoucherDetailId"]);
                //ogroup.VoucherId = VoucherId;
                //ogroup.ProductId = Format.ConvertToInt(data["ProductId"]);
                //ogroup.ProductCode = data["ProductCode"].ToString();
                //ogroup.ProductName = data["ProductName"].ToString();
                //ogroup.Quantity = Format.ConvertTodecimal(data["Quantity"]);
                //ogroup.UnitId = Format.ConvertToInt(data["UnitId"]);
                //ogroup.UnitPrice = Format.ConvertTodecimal(data["UnitPrice"]);
                //ogroup.VATPercent = Format.ConvertTodecimal(data["VATPercent"]);
                //ogroup.VATAmount = Format.ConvertTodecimal(data["VATAmount"]);
                //ogroup.DiscountPercent = Format.ConvertTodecimal(data["DiscountPercent"]);
                //ogroup.DiscountAmount = Format.ConvertTodecimal(data["DiscountAmount"]);
                //ogroup.Amount = Format.ConvertTodecimal(data["Amount"]);
                //ogroup.TotalAmount = Format.ConvertTodecimal(data["TotalAmount"]);
                //ogroup.IsInput = true;
                //ogroup.WarehouseId = Voucher.WarehouseId;
                //VoucherDetailService svDetail = VoucherDetailService.GetInstance();
                //VoucherDetailEntity resul = svDetail.InsertUpdate(ogroup);
                //if (isSaveMaster)
                //{
                //    BindingDocument();
                //    return -1;
                //}
                //else
                //    return resul.VoucherDetailId;

            }
            catch (Exception ex)
            {
                throw ex;

            }
        }
        private VoucherEntity GetVoucherEntity()
        {
            VoucherEntity voucherEntity = new VoucherEntity();
            voucherEntity.VoucherNo = txtVoucherNo.Text.Trim();
            if (dfVoucherDate.SelectedValue != null)
                voucherEntity.VocherDate = dfVoucherDate.SelectedDate;
            else voucherEntity.VocherDate = DateTime.Now;
            voucherEntity.BranchId = 1;
            if (!string.IsNullOrEmpty(cboObject.SelectedItem.Value))
                voucherEntity.ObjectId = Format.ConvertToInt(cboObject.SelectedItem.Value);
            if (!string.IsNullOrEmpty(cboPaymentMethod.SelectedItem.Value))
                voucherEntity.PaymentMethodId = Format.ConvertToInt(cboPaymentMethod.SelectedItem.Value);
            if (!string.IsNullOrEmpty(cboMaturity.SelectedItem.Value))
                voucherEntity.MaturityId = Format.ConvertToInt(cboMaturity.SelectedItem.Value);
            //           if (!string.IsNullOrEmpty(cboCurrency.SelectedItem.Value))
            //                voucherEntity.CurrencyId = Format.ConvertToInt(cboCurrency.SelectedItem.Value);
            //           if (!string.IsNullOrEmpty(nfRate.Text))
            //               voucherEntity.CurrencyRate = Format.ConvertToInt(nfRate.Value);
            if (!string.IsNullOrEmpty(cboInputMethod.SelectedItem.Value))
                voucherEntity.InOutMethodId = Format.ConvertToInt(cboInputMethod.SelectedItem.Value);
            if (!string.IsNullOrEmpty(cboWarehouse.SelectedItem.Value))
                voucherEntity.WarehouseId = Format.ConvertToInt(cboWarehouse.SelectedItem.Value);
            voucherEntity.CreatedBy = FSSContext.Current.User.EmployeeId;
            voucherEntity.CreatedDate = DateTime.Now;
            voucherEntity.UpdatedBy = FSSContext.Current.User.EmployeeId;
            voucherEntity.UpdatedDate = DateTime.Now;
            return voucherEntity;
        }
        protected void grdMain_OnCommand(object sender, DirectEventArgs e) { }
        public void btnSaveExit_Click(object sender, DirectEventArgs e) { }
        public void btnSave_Click(object sender, DirectEventArgs e) { }
        public void btnPrint_Click(object sender, DirectEventArgs e) { }
        public void btnExit_Click(object sender, DirectEventArgs e) { }
    }
}
